package com.dhwl.hsh.driver.ui.activity;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.dhwl.hsh.lib.constant.ServerConstant;
import com.dhwl.hsh.lib.utils.StringUtils;
import com.dhwl.hsh.driver.R;
import com.dhwl.hsh.driver.common.Constant;
import com.dhwl.hsh.driver.view.DeleteableEditText;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * 重置登录密码页面
 *
 * @author Derek.Kidd
 * @version [版本号, 2015-4-16]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ResetLoginPwdActivity extends BaseActivity {
    @Bind(R.id.edit_reset_login_pwd1)
     DeleteableEditText editLoginPwd1;

    @Bind(R.id.edit_reset_login_pwd2)
     DeleteableEditText editLoginPwd2;

    @Bind(R.id.btn_reset_login_next)
     Button btnNext;

    @Bind(R.id.btn_loading)
     Button btnLoading;

    @Bind(R.id.imageViewLoading)
     ImageView imageLoading;

    //完成按钮的动画效果钮的动画效果
    private AnimationDrawable anim;

    private String phoneNum;

    private String firstNewPwd;

    private String secondNewPwd;

    private String code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleContent(R.string.reset_pwd);




    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reset_login_pwd;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        anim = (AnimationDrawable) imageLoading.getBackground();
        editLoginPwd1.setMyTextWatcher(new DeleteableEditText.MyTextWatcher() {
            @Override
            public void afterTextChanged() {
                showSubmitBtn();
            }
        });
        editLoginPwd2.setMyTextWatcher(new DeleteableEditText.MyTextWatcher() {
            @Override
            public void afterTextChanged() {
                showSubmitBtn();
            }
        });
    }


    @Override
    protected void initData() {
        phoneNum = getIntent().getExtras().getString(Constant.UserInfo.KEY_PHONE_NUM);
    }


    /**
     * 控制提交按钮是否可以使用
     *
     * @see [类、类#方法、类#成员]
     */
    private void showSubmitBtn() {
        firstNewPwd = editLoginPwd1.getText();
        secondNewPwd = editLoginPwd2.getText();

        if (StringUtils.isEmpty(firstNewPwd)
                || StringUtils.isEmpty(secondNewPwd)
                || (firstNewPwd.length() < Constant.Rule.PWD_MIN_LENGTH || firstNewPwd.length() > Constant.Rule.PWD_MAX_LENGTH)
                || (secondNewPwd.length() < Constant.Rule.PWD_MIN_LENGTH || secondNewPwd.length() > Constant.Rule.PWD_MAX_LENGTH)) {
            btnNext.setBackgroundResource(R.drawable.comm_bg_btn_grey);
            btnNext.setEnabled(false);
        } else {
            btnNext.setBackgroundResource(R.drawable.selector_comm_btn);
            btnNext.setEnabled(true);
        }
    }

    @OnClick(R.id.btn_reset_login_next)
    public void onNextActionClick(View v) {
        // 获取密码、验证
        firstNewPwd = editLoginPwd1.getText();
        secondNewPwd = editLoginPwd2.getText();

        if (StringUtils.isEmpty(firstNewPwd) || StringUtils.isEmpty(secondNewPwd)) {
            showToast(R.string.user_login_validate_pwd_is_null);
            return;
        }

        // 密码长度验证
        if (firstNewPwd.length() < Constant.Rule.PWD_MIN_LENGTH || firstNewPwd.length() > Constant.Rule.PWD_MAX_LENGTH) {
            showToast(getString(R.string.user_login_pwd_validate_length,
                    Constant.Rule.PWD_MIN_LENGTH,
                    Constant.Rule.PWD_MAX_LENGTH));
            return;
        }
        if (secondNewPwd.length() < Constant.Rule.PWD_MIN_LENGTH
                || secondNewPwd.length() > Constant.Rule.PWD_MAX_LENGTH) {
            showToast(getString(R.string.user_login_pwd_validate_length,
                    Constant.Rule.PWD_MIN_LENGTH,
                    Constant.Rule.PWD_MAX_LENGTH));
            return;
        }
        //两次新的密码是否一致
        if (!TextUtils.equals(firstNewPwd, secondNewPwd)) {
            showToast(R.string.toast_confirm_identity_pwd);
            return;
        }
        // 密码太简单
        if (StringUtils.equalStr(firstNewPwd) || StringUtils.isOrderNumeric(firstNewPwd)
                || StringUtils.isOrderNumeric_(firstNewPwd)) {
            showToast(R.string.toast_password_easy);
        }

        resetPwdHttpRequest();
        btnLoading.setVisibility(View.GONE);
        imageLoading.setVisibility(View.GONE);
        btnNext.setVisibility(View.VISIBLE);
    }


    private void resetPwdHttpRequest() {
        //        showWaitingDialog();
        //重置密码等待
//        btnLoading.setVisibility(View.VISIBLE);
//        imageLoading.setVisibility(View.VISIBLE);
//        btnNext.setVisibility(View.GONE);
//        anim.start();

        //                    hideWaitingDialog();
        //                    //重置密码等待结束
        //                    btnLoading.setVisibility(View.GONE);
        //                    imageLoading.setVisibility(View.GONE);
        //                    btnNext.setVisibility(View.VISIBLE);
        showToast(R.string.toast_reset_pwd_success);

        //返回登录页面
        startActivity(new Intent(ResetLoginPwdActivity.this, LoginActivity.class));
        Intent intent = new Intent();
        intent.putExtra(Constant.UserInfo.KEY_PHONE_NUM, phoneNum);
        finish();
    }
}